
public class Sort {
    public void insertsort(int[] array){
        for (int i = 1; i <array.length ; i++) {
            int tmp=array[i];
            int j = i-1;
            for (; j >0 ; j--) {
                if(array[i]<array[j]){
                    array[j+1]=array[j];
                }
                else {
                    //array[j]=tmp;
                    break;
                }
            }
            array[j+1]=tmp;


        }
    }
    public static void swapWithTemp(int[] array,int num1,int num2) {
        int temp = array[num1];
        array[num1]=array[num2];
        array[num2] = temp;
    }

    public static void Selectsort(int[] array){

        for (int i = 0; i <array.length ; i++) {
            int tmp=i;

            for (int j=i+1; j <array.length ;j++) {
                if(array[j]<array[tmp]) {
                    tmp =j;
                }
            }
            swapWithTemp(array,tmp,i);
        }
    }
    //前后指针left right
    public static void Selectsort2(int[] array){
        int left=0;
        int right=array.length;
        while(left<right){
            int maxindex=left;
            int minindex=left;
            for (int i = 0; i <right; i++) {
                if(array[i]>array[maxindex]){
                    maxindex=i;
                }
                if(array[i]<array[minindex])
                    minindex=i;
            }
            swapWithTemp(array,minindex,left);
            if(maxindex==left){
                minindex=maxindex;
            }
            swapWithTemp(array,maxindex,right);
            left++;
            right--;

        }
    }
    public static void bubblesort(int[] array){
        for (int i = 0; i <array.length-1 ; i++) {
            boolean flg=false;
            for (int j = 0; j <array.length-i-1 ; j++) {
                if(array[j]>array[j+1]){
                    swapWithTemp(array,j,j+1);
                }
                flg=true;
            }
            if (flg==false){
                break;
            }
        }
    }

    }




